﻿@{
    ViewBag.Title = "RoleSet";
}
<div class="layui-table-tool" style="min-height:unset; padding-left:0; padding-right:0;margin-bottom:-10px">
    <div class="layui-btn-container" style="padding-left:10px;">
        <a id="expand" class="layui-btn layui-btn-sm">全部展开</a>
        <a id="fold" class="layui-btn layui-btn-sm">全部收起</a>
        <a id="save" class="layui-btn layui-btn-sm">权限保存</a>
    </div>
</div>
<table id="table"></table>
<script>
    layui.config({
        base: '/Content/lib/lay-module/'
    }).extend({
        treeTable: 'treeTable/treeTable'
    }).use(['treeTable'], function () {
        var $ = layui.jquery;
        var treeTable = layui.treeTable;
        // 渲染表格
        var treeTb = treeTable.render({
            elem: '#table',
            even: true,
            tree: {
                iconIndex: 1,  // 折叠图标显示在第几列
                idName: 'ID',  // 自定义id字段的名称
                pidName: 'ParentID',  // 自定义标识是否还有子节点的字段名称
                isPidData: true  // 是否是pid形式数据
            },
            cols: [
                { type: 'checkbox', width: 40 },
                { field: 'ModuleName', minWidth: 200, title: '模块名称' },
                {
                    field: 'ModuleType', width: 100, align: 'center', templet: function (d) {
                        if (d.ModuleType == 2) {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        }
                        if (d.ModuleType == 0) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '模块类型'
                }
            ],
            reqData: function (data, callback) {
                //在这里写ajax请求，通过callback方法回调数据
                $.ajax({
                    url: '/SysModule/List',
                    success: function (res) {
                        callback(res.data);
                        treeTb.setChecked([@Html.Raw(ViewBag.moduleIDs)]);
                    }
                });
            }
        });

        $("#expand").click(function () {
            treeTb.expandAll();
        })
        $("#fold").click(function () {
            treeTb.foldAll();
        })
        $("#save").click(function () {
            var modules = "";
            $.each(treeTb.checkStatus(), function (index, item) {
                modules += item.ID + "," + (item.isIndeterminate ? 1 : 0) + "|";
            })
            $.ajax({
                url: 'RoleSet',
                data: { roleID:@ViewBag.roleID, modules: modules },
                success: function (res) {
                    layer.alert(res.msg, function (index) {
                        layer.close(index);
                        if (res.count > 0) {
                            parent.layer.closeAll();
                        }
                    })
                }
            });
        })
    });
</script>